import React, {useEffect, useState} from "react";
import {Redirect, Route, Switch} from "react-router-dom";
import Home from "../../views/newssandbox/home/Home";
import UserList from "../../views/newssandbox/user-manage/UserList";
import RoleList from "../../views/newssandbox/right-manage/RoleList";
import RightList from "../../views/newssandbox/right-manage/RightList";
import NoPermission from "../../views/newssandbox/nopermission/NoPermission";
import NewsAdd from "../../views/newssandbox/news-manage/NewsAdd";
import NewsDraft from "../../views/newssandbox/news-manage/NewsDraft";
import NewsCategory from "../../views/newssandbox/news-manage/NewsCategory";
import Audit from "../../views/newssandbox/audit-manage/Audit";
import AuditList from "../../views/newssandbox/audit-manage/AuditList";
import Unpublished from "../../views/newssandbox/publish-manage/Unpublished";
import Published from "../../views/newssandbox/publish-manage/Published";
import Sunset from "../../views/newssandbox/publish-manage/Sunset";
import axios from "axios";
import NewsPreview from "../../views/newssandbox/news-manage/NewsPreview";
import NewsUpdate from "../../views/newssandbox/news-manage/NewsUpdate";

const LocalRouterMap = {
    "/home": Home,
    "/user-manage/list": UserList,
    "/right-manage/role/list": RoleList,
    "/right-manage/right/list": RightList,
    "/news-manage/add": NewsAdd,
    "/news-manage/draft": NewsDraft,
    "/news-manage/category": NewsCategory,
    "/audit-manage/audit": Audit,
    "/audit-manage/list": AuditList,
    "/publish-manage/unpublished": Unpublished,
    "/publish-manage/published": Published,
    "/publish-manage/sunset": Sunset,
    "/news-manage/preview/:id":NewsPreview,
    "/news-manage/update/:id":NewsUpdate
}
export default function NewsRouter() {
    const [backRouteList, setBackRouteList] = useState([])
    useEffect(async () => {
        let res = await Promise.all([
            axios.get(`http://localhost:5000/rights`),
            axios.get(`http://localhost:5000/children`)
        ])
        // console.log(res)
        setBackRouteList([...res[0].data, ...res[1].data])
        // console.log(backRouteList)
    }, [])
    const {role:{rights}} = JSON.parse(localStorage.getItem("token"))
    const checkRoute = (item) => {
        return LocalRouterMap[item.key] && item.pagepermisson || item.routepermisson
    }
    const checkUserPermission = (item) => {
        return rights.includes(item.key)
    }


    return (
        <div>
            <Switch>
                {
                    backRouteList.map(item => {
                            if (checkRoute(item) && checkUserPermission(item)) {
                                return <Route path={item.key} key={item.key} component={LocalRouterMap[item.key]}
                                              exact={true}/>
                            }else {
                                return null
                            }

                        }
                    )
                }
                {/* 什么都匹配不到就会匹配以下组件 */}
                <Redirect from={"/"} to={"/home"} exact={true}/>
                {
                    backRouteList.length > 0 && <Route path={"*"} component={NoPermission}/>
                }
            </Switch>
        </div>
    )
}
